Types of Light Dependent Resistors
Applications
/* Name : main.c
* Purpose : Source code for LDR Interfacing with PIC18F4550.
* Author : Gemicates
* Date : 2017-07-03
* Website : www.gemicates.org
* Revision : None
*/
#include<htc.h>
#define _XTAL_FREQ 12000000 // 12MHZ
#define LDR PORTA
void LDR_Init()
{
ADCON0 = 0x41; // ADC Module Turned ON and Clock is selected
ADCON1 = 0xC0; // All pins as Analog Input
// With reference voltages VDD and VSS
}
unsigned int LDR_Read(unsigned char channel)
{
if(channel > 7) // If Invalid channel selected
return 0; // Return 0
ADCON0 &= 0xC5; // Clearing the Channel Selection Bits
ADCON0 |= channel<<3; // Setting the required Bits
__delay_ms(2); // Acquisition time to charge hold capacitor
GO_nDONE = 1; // Initializes A/D Conversion
while(GO_nDONE); // Wait for A/D Conversion to complete
return ((ADRESH<<8)+ADRESL); // Returns Result
}
void main()
{
unsigned int a;
TRISC= 0xC0; // PORTC as output
LDR = 0xFF; // PORTA as input
LDR_Init(); // Initializes LDR Module
do
{
a = LDR_Read(0); // Reading Analog Channel 0
PORTC = a>>8; // Higher 2 bits to PORTC
__delay_ms(60); // Delay
}while(1); // Infinite Loop
}